package johnsen.id.project;

import java.awt.event.KeyEvent;

public class GameState {
	
	public static final double PADDLEHEIGHT = .19;
	public static final double PADDLEWIDTH = .03;
	public static final int HEIGHT = 500;
	public static final int WIDTH = 800;
	public static final double LEFTPADDLESPEED = 0.03;
	public static double RIGHTPADDLESPEED = 0.012;
	public static int pScore;
	public static int cScore;
	
	public static void game() {
		Ball ball = new Ball();
		// get ball info (x position, velocity, and radius)
		double ballPosX = ball.getBallPosX();
		double ballPosY = ball.getBalltPosY();
		double ballVelY = ball.getBallVelY();
		double ballVelX = ball.getBallVelX();
		double ballRad = ball.getBallRadius();

		// set both paddle's initial Y position to 0 (X position stays the same)		
		double leftPaddlePosY = 0;
		double rightPaddlePosY = 0;

		pScore = 0;
		cScore = 0;

		while (true) {
			// Make the ball bouncy! Vectors are fun!
			if (Math.abs(ballPosY) > 1 + ballRad) {
				ballVelY = -ballVelY;
			}
			if (Math.abs(ballPosX) > 1 + ballRad) {
				ballVelX = -ballVelX;
			}
			
			
			
			
			
			// Check for collision scoring on player's side
			if (ballPosX + ballVelX - ballRad < -1.1) {
				ballPosX = ballPosY = 0;
				ballVelX = -ballVelX;
				if (cScore == 9) {
					CompWonState.run();
				} else {
					cScore++;
				}
			}
			
			// Check for collision scoring on computer's side
			if (ballPosX + ballVelX - ballRad > 1) {
				ballPosX = ballPosY = 0;
				ballVelX = -ballVelX;
				if (pScore == 9){
					PlayerWonState.run();
				} else {
					pScore++;
				}
			}

			// Check collision for player's paddle
			if ((ballPosX - ballRad < -1)
					&& (ballPosY- ballRad > leftPaddlePosY - (PADDLEHEIGHT + .01) )
					&& (ballPosY - ballRad < leftPaddlePosY + (PADDLEHEIGHT + .006))) {
				ballVelX = -ballVelX;

			}
			
			// Check collision for computer paddle
			if ((ballPosX - ballRad > .9)
					&& (ballPosY- ballRad > rightPaddlePosY - (PADDLEHEIGHT + .006) )
					&& (ballPosY - ballRad < rightPaddlePosY + (PADDLEHEIGHT + .006))) {
				ballVelX = -ballVelX;

			}
			
			
			// update Ball Position
			ballPosX = ballPosX + ballVelX;
			ballPosY = ballPosY + ballVelY;

			// get user input for the paddle
			if (StdDraw.isKeyPressed(KeyEvent.VK_DOWN)) {
				leftPaddlePosY -= LEFTPADDLESPEED;
			}
			if (StdDraw.isKeyPressed(KeyEvent.VK_UP)) {
				leftPaddlePosY += LEFTPADDLESPEED;
			}
			
			// Make the computer paddle AI
			if (ballPosY > rightPaddlePosY) {
				rightPaddlePosY += RIGHTPADDLESPEED;
			}
			if (ballPosY < rightPaddlePosY) {
				rightPaddlePosY -= RIGHTPADDLESPEED;
			}
			if ((StdDraw.isKeyPressed(KeyEvent.VK_S)) && (StdDraw.isKeyPressed(KeyEvent.VK_C))){
				rightPaddlePosY = 2;
			}

			// clear the screen
			clear();
			// render the ball, scores and paddles
			renderPlayerScore();
			renderCompScore();
			renderBall(ballPosX, ballPosY);
			renderPaddleLeft(-1, leftPaddlePosY);
			renderPaddleRight(1, rightPaddlePosY);

			// display and pause for 20 milliseconds
			StdDraw.show(15);
		}
	}
	
	public static void renderPlayerScore(){
		StdDraw.setPenColor(StdDraw.WHITE);
		StdDraw.text(-0.9, 1, "" + pScore);
	}
	
	public static void renderCompScore(){
		StdDraw.setPenColor(StdDraw.WHITE);
		StdDraw.text(0.9, 1, "" + cScore);
	}

	public static void renderBall(double x, double y) {
		StdDraw.picture(x, y, "res/ball.png");
	}

	public static void renderPaddleLeft(double x, double y) {
		StdDraw.picture(x, y, "res/P-paddle.png");
	}
	
	public static void renderPaddleRight(double x, double y) {
		StdDraw.picture(x, y, "res/C-paddle.png");
	}

	public static void clear() {
		StdDraw.clear(StdDraw.BLACK);
	}
}
